package com.boot.web.serviceImpl;

import com.boot.web.dao.*;
import com.boot.web.entity.*;
import com.boot.web.service.OrdersService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.boot.web.service.OrdersCopy30Service;
import com.boot.base.serviceImpl.BaseServiceImpl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 重新下订单业务层接口实现
 * @author 【zhouJie】
 * @date 2021-10-26 09:49:32
 */
@Service
public class OrdersCopy30ServiceImpl extends BaseServiceImpl<OrdersCopy30> implements OrdersCopy30Service {

    private static final Logger logger = LoggerFactory.getLogger(OrdersCopy30ServiceImpl.class);

    @Autowired
    private OrdersCopy30Dao ordersCopy30Dao;

    @Autowired
    private CollageModeDao collageModeDao;

    @Autowired
    private ProductDao productDao;

    @Autowired
    private ModeProductDao modeProductDao;

    @Autowired
    private CollageUserDao collageUserDao;

    @Autowired
    private CustomerDao customerDao;

    @Autowired
    private AccountDao accountDao;

    @Autowired
    private OrdersDao ordersDao;

    @Autowired
    private OrderDetailsDao orderDetailsDao;

    @Autowired
    private FailRedPackDao failRedPackDao;

    @Autowired
    private CollageInRecord30Dao collageInRecord30Dao;

    @Autowired
    private TodayPrizeDao todayPrizeDao;

    @Autowired
    private GoodsOrdersDao goodsOrdersDao;

    @Autowired
    private StoreShopDao storeShopDao;

    @Autowired
    private ClassifyDao classifyDao;

    @Autowired
    private AccountRecordDao accountRecordDao;

    @Autowired
    private SpecAttributeDao specAttributeDao;

    @Autowired
    private SkuRelationDao skuRelationDao;

    @Autowired
    private UserCouponDao userCouponDao;

    @Autowired
    private CollageInRecord30CopyDao collageInRecord30CopyDao;

    @Autowired
    private TodayCollageVoucherDao todayCollageVoucherDao;

    @Autowired
    private CollageTotalVoucherDao collageTotalVoucherDao;

    @Autowired
    private OrdersService ordersService;

    @Override
    public void handleAgainOrders(Integer collageModeId, Integer productId, Integer userId, BigDecimal payAmount, String consigneeName,
                                  String fullAddress, String consigneePhone, BigDecimal freight, Integer skuId, String outTradeNo, String tradeNo) {

        CollageMode collageMode = collageModeDao.queryById(collageModeId);

        Product product = productDao.queryById(productId);

        Integer couponId_1 = 3; // 默认为30
        if(product.getCollageModeId() == 40){
            couponId_1 = 4; // 为40
        }
        if(product.getCollageModeId() == 30 || product.getCollageModeId() == 40) {
            UserCoupon userCoupon = userCouponDao.queryByUserIdAndCouponId(userId, couponId_1);
            // 扣除参团券一张
            userCoupon.setQuantity(userCoupon.getQuantity() - 1);
            userCouponDao.update(userCoupon);
        }

        SkuRelation sku = skuRelationDao.queryById(skuId);

        Integer buyQuantity = 1;

        Customer customer = customerDao.queryById(userId);

        Date date = new Date();

        // 处理团信息
        ModeProduct modeProduct = modeProductDao.queryByProductIdByStatus(product.getId());
        if(modeProduct == null){
            // 创建新团
            ModeProduct newMP = new ModeProduct();
            newMP.setModeId(product.getCollageModeId());
            newMP.setProductId(product.getId());
            newMP.setStatus("0");
            newMP.setUpdateTime(date);
            modeProductDao.save(newMP);
            // 加入到当前团
            CollageUser newCU = new CollageUser();
            newCU.setModeProductId(newMP.getId());
            newCU.setUserId(customer.getId());
            newCU.setUpdateTime(date);
            collageUserDao.save(newCU);

            // TODO 创建订单数据
            createOrdersInfo(outTradeNo, tradeNo, payAmount, freight, product, collageMode.getName(), newMP.getId(), customer.getId(),
                    consigneeName, consigneePhone, fullAddress, customer.getIsMember(), sku, buyQuantity, date);

        }else{
            int quantity = collageMode.getPeopleQuantity(); // 20
            // 查询现在这个产品团里面有多少人了
            List<CollageUser> collageUserList = collageUserDao.queryByModeProductId(modeProduct.getId());
            int rowSize = collageUserList.size(); // 已参与人数
            if(rowSize < (quantity -1)){
                // 直接加入
                CollageUser newCU = new CollageUser();
                newCU.setModeProductId(modeProduct.getId());
                newCU.setUserId(customer.getId());
                newCU.setUpdateTime(date);
                collageUserDao.save(newCU);

                // TODO 创建订单数据
                createOrdersInfo(outTradeNo, tradeNo, payAmount, freight, product, collageMode.getName(), modeProduct.getId(), customer.getId(),
                        consigneeName, consigneePhone, fullAddress, customer.getIsMember(), sku, buyQuantity, date);

            }else if(rowSize == (quantity -1)){ // 需要重新生成新团
                // 加入当前团
                CollageUser newCU = new CollageUser();
                newCU.setModeProductId(modeProduct.getId());
                newCU.setUserId(customer.getId());
                newCU.setUpdateTime(date);
                collageUserDao.save(newCU);
                // 修改老团,拼团成功
                modeProduct.setStatus("1");
                modeProductDao.update(modeProduct);
                // 创建新团
                ModeProduct newMo = new ModeProduct();
                newMo.setUpdateTime(new Date());
                newMo.setProductId(product.getId());
                newMo.setModeId(collageMode.getId());
                newMo.setStatus("0");
                modeProductDao.save(newMo);

                // TODO 创建订单数据
                createOrdersInfo(outTradeNo, tradeNo, payAmount, freight, product, collageMode.getName(), modeProduct.getId(), customer.getId(),
                        consigneeName, consigneePhone, fullAddress, customer.getIsMember(), sku, buyQuantity, date);

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String today = sdf.format(new Date());

                // 获取当前团的订单信息，处理哪些需要发货，哪些需要退款，哪些需要挪团
                List<Orders> ordersList = ordersDao.queryOrdersByCollageProductId(modeProduct.getId());
                int ordersSize = ordersList.size();
                if(ordersSize > 0){
                    for(int u = 0; u < ordersSize; u++){
                        Orders o = ordersList.get(u);
                        Customer cu = customerDao.queryById(o.getOrderPersonId());
                        OrderDetails od = orderDetailsDao.queryByOrderNo(o.getOrderNo());
                        Product p = productDao.queryById(od.getProductId());
                        //Account cuAcc = accountDao.queryAccountByUserId(cu.getId());
                        //Integer couponId = 3; // 默认100
                        //if(p.getCollageModeId() == 40){ // 为200
                        //couponId = 4;
                        //}
                        //UserCoupon uc3 = userCouponDao.queryByUserIdAndCouponId(cu.getId(), couponId);
                        // 判断今日是否中过奖
                        //TodayPrize tp = todayPrizeDao.queryByUserIdAndToday(cu.getId(), today);
                        //if(tp == null){
                        // 创建今日记录
                        //TodayPrize newTp = new TodayPrize();
                        //newTp.setIsPrize("0");
                        //newTp.setUpdateTime(date);
                        //newTp.setUserId(cu.getId());
                        //newTp.setFrequency(1);
                        //todayPrizeDao.save(newTp);
                        // 没有中
                        // 继续判断 cu 是不是会员
                        if("0".equals(cu.getIsMember())){
                            // 非会员会员 直接中
                            //newTp.setIsPrize("1");
                            //todayPrizeDao.update(newTp);
                            // 发货
                            o.setDealStatus("1");
                            ordersDao.update(o);
                            // 获取累计中奖记录数据
                            CollageInRecord30 cir30 = collageInRecord30Dao.queryByModeIdAndUserId(o.getCollageModeId(), cu.getId());
                            if(cir30 == null){
                                // 创建cir30
                                CollageInRecord30 newCir30 = new CollageInRecord30();
                                newCir30.setModeId(o.getCollageModeId());
                                newCir30.setUserId(cu.getId());
                                newCir30.setStatus("0");
                                newCir30.setTotalQuantity(1);
                                newCir30.setUpdateTime(date);
                                newCir30.setPayAmount(o.getPayAmount());
                                collageInRecord30Dao.save(newCir30);
                                // 添加详情
                                CollageInRecord30Copy newCir30Copy = new CollageInRecord30Copy();
                                newCir30Copy.setIsTack("1"); // 提了
                                newCir30Copy.setCir30Id(newCir30.getId());
                                newCir30Copy.setOrderNo(o.getOrderNo());
                                newCir30Copy.setPayAmount(o.getPayAmount());
                                collageInRecord30CopyDao.save(newCir30Copy);
                            }else{
                                // 判断是否需要返钱
                                if(cir30.getTotalQuantity() == 5){
                                    // 开始退钱，退未提货的钱
                                    Account cuAcc = accountDao.queryAccountByUserId(cu.getId());
                                    Double d = 0.0;
                                    List<CollageInRecord30Copy> cir30CopyList = collageInRecord30CopyDao.queryByCir30Id(cir30.getId());
                                    if(cir30CopyList.size() > 0){
                                        for(CollageInRecord30Copy cir30Copy : cir30CopyList){
                                            cir30Copy.setIsTack("1"); // 修改为已提货
                                            d += cir30Copy.getPayAmount().doubleValue();
                                            collageInRecord30CopyDao.update(cir30Copy);
                                            Orders orders = ordersDao.queryByOrderNo(cir30Copy.getOrderNo());
                                            orders.setDealStatus("3"); // 已完成
                                            orders.setConfirmTime(new Date());
                                            orders.setRemarks("拼团专区内累计拼中六次! 当前订单自动完成");
                                            ordersDao.update(orders);
                                        }
                                    }
                                    cuAcc.setVoucherBalance(cuAcc.getVoucherBalance().add(BigDecimal.valueOf(d)));
                                    accountDao.update(cuAcc);
                                    // 加个ar记录
                                    AccountRecord ar = new AccountRecord();
                                    ar.setIntoUserId(cu.getId());
                                    ar.setUpdateTime(date);
                                    ar.setType("0");
                                    ar.setAmount(BigDecimal.valueOf(d));
                                    ar.setNowAmount(cuAcc.getVoucherBalance());
                                    ar.setSign("1");
                                    ar.setOrderNo(o.getOrderNo());
                                    ar.setInfo("拼团专区内累计拼中六次! 广告补贴：" + d);
                                    accountRecordDao.save(ar);
                                    // 改为退完
                                    cir30.setStatus("1"); // 已退
                                    collageInRecord30Dao.update(cir30);
                                    // 把第六次做为新的第一次并发货
                                    // 发货
                                    o.setDealStatus("1");
                                    ordersDao.update(o);
                                    // 创建cir30
                                    CollageInRecord30 newCir30 = new CollageInRecord30();
                                    newCir30.setModeId(o.getCollageModeId());
                                    newCir30.setUserId(cu.getId());
                                    newCir30.setStatus("0");
                                    newCir30.setTotalQuantity(1);
                                    newCir30.setUpdateTime(date);
                                    newCir30.setPayAmount(o.getPayAmount());
                                    collageInRecord30Dao.save(newCir30);
                                    // 添加详情
                                    CollageInRecord30Copy newCir30Copy = new CollageInRecord30Copy();
                                    newCir30Copy.setIsTack("1"); // 提了
                                    newCir30Copy.setCir30Id(newCir30.getId());
                                    newCir30Copy.setOrderNo(o.getOrderNo());
                                    newCir30Copy.setPayAmount(o.getPayAmount());
                                    collageInRecord30CopyDao.save(newCir30Copy);
                                }else{
                                    // 继续累计
                                    cir30.setTotalQuantity(cir30.getTotalQuantity() + 1);
                                    cir30.setPayAmount(cir30.getPayAmount().add(o.getPayAmount()));
                                    collageInRecord30Dao.update(cir30);
                                    // 添加详情
                                    CollageInRecord30Copy newCir30Copy = new CollageInRecord30Copy();
                                    newCir30Copy.setIsTack("0"); // 没有提
                                    newCir30Copy.setCir30Id(cir30.getId());
                                    newCir30Copy.setOrderNo(o.getOrderNo());
                                    newCir30Copy.setPayAmount(o.getPayAmount());
                                    collageInRecord30CopyDao.save(newCir30Copy);
                                    // 代售
                                    o.setDealStatus("7");
                                    ordersDao.update(o);
                                }
                            }
                        }else if("1".equals(cu.getIsMember())){
                            // 会员今日每中 直接一波流出局
                            //newTp.setIsPrize("1");
                            //newTp.setFrequency(30);
                            //todayPrizeDao.update(newTp);
                            // 发货
                            o.setDealStatus("1");
                            ordersDao.update(o);
                            // 那29元
                            BigDecimal profit = BigDecimal.valueOf(29);
                            if(p.getCollageModeId() == 40){
                                //拿走58元
                                profit = BigDecimal.valueOf(58);
                            }
                            TodayCollageVoucher tcv = todayCollageVoucherDao.queryByUserIdAndModeId(cu.getId(), p.getCollageModeId());
                            if(tcv == null){
                                TodayCollageVoucher newTcv = new TodayCollageVoucher();
                                newTcv.setAmount(profit);
                                newTcv.setUserId(cu.getId());
                                newTcv.setStatus("0");
                                newTcv.setIsRefund("0");
                                newTcv.setUpdateTime(date);
                                newTcv.setModeId(p.getCollageModeId());
                                todayCollageVoucherDao.save(newTcv);
                                // 分润
                                ordersService.handleCollageProfit30(cu.getId(), profit, o.getOrderNo());
                            }else{
                                // 第六次不累计了，作为新的一次开始
                                if(tcv.getAmount().doubleValue() == 145 || tcv.getAmount().doubleValue() == 290){
                                    tcv.setStatus("1");
                                    todayCollageVoucherDao.update(tcv);
                                    TodayCollageVoucher newTcv1 = new TodayCollageVoucher();
                                    newTcv1.setAmount(profit);
                                    newTcv1.setUserId(cu.getId());
                                    newTcv1.setStatus("0");
                                    newTcv1.setIsRefund("0");
                                    newTcv1.setUpdateTime(new Date());
                                    newTcv1.setModeId(p.getCollageModeId());
                                    todayCollageVoucherDao.save(newTcv1);
                                    // 分润
                                    ordersService.handleCollageProfit30(cu.getId(), profit, o.getOrderNo());
                                }else{
                                    tcv.setAmount(tcv.getAmount().add(profit));
                                    todayCollageVoucherDao.update(tcv);
                                    // 分润
                                    ordersService.handleCollageProfit30(cu.getId(), profit, o.getOrderNo());
                                }
                            }
                            // 获取累计中奖记录数据
                            CollageInRecord30 cir30 = collageInRecord30Dao.queryByModeIdAndUserId(o.getCollageModeId(), cu.getId());
                            if(cir30 == null){
                                // 创建cir30
                                CollageInRecord30 newCir30 = new CollageInRecord30();
                                newCir30.setModeId(o.getCollageModeId());
                                newCir30.setUserId(cu.getId());
                                newCir30.setStatus("0");
                                newCir30.setTotalQuantity(1);
                                newCir30.setUpdateTime(date);
                                newCir30.setPayAmount(o.getPayAmount());
                                collageInRecord30Dao.save(newCir30);
                                // 添加详情
                                CollageInRecord30Copy newCir30Copy = new CollageInRecord30Copy();
                                newCir30Copy.setIsTack("1"); // 提了
                                newCir30Copy.setCir30Id(newCir30.getId());
                                newCir30Copy.setOrderNo(o.getOrderNo());
                                newCir30Copy.setPayAmount(o.getPayAmount());
                                collageInRecord30CopyDao.save(newCir30Copy);
                            }else{
                                // 判断是否需要返钱
                                if(cir30.getTotalQuantity() == 5){
                                    // 开始退钱，退未提货的钱
                                    Account cuAcc = accountDao.queryAccountByUserId(cu.getId());
                                    Double d = 0.0;
                                    List<CollageInRecord30Copy> cir30CopyList = collageInRecord30CopyDao.queryByCir30Id(cir30.getId());
                                    if(cir30CopyList.size() > 0){
                                        for(CollageInRecord30Copy cir30Copy : cir30CopyList){
                                            cir30Copy.setIsTack("1"); // 修改为已提货
                                            d += cir30Copy.getPayAmount().doubleValue();
                                            collageInRecord30CopyDao.update(cir30Copy);
                                            Orders orders = ordersDao.queryByOrderNo(cir30Copy.getOrderNo());
                                            orders.setDealStatus("3"); // 已完成
                                            orders.setConfirmTime(new Date());
                                            orders.setRemarks("拼团专区内累计拼中六次! 当前订单自动完成");
                                            ordersDao.update(orders);
                                        }
                                    }
                                    cuAcc.setVoucherBalance(cuAcc.getVoucherBalance().add(BigDecimal.valueOf(d)));
                                    accountDao.update(cuAcc);
                                    // 加个ar记录
                                    AccountRecord ar = new AccountRecord();
                                    ar.setIntoUserId(cu.getId());
                                    ar.setUpdateTime(date);
                                    ar.setType("8");
                                    ar.setAmount(BigDecimal.valueOf(d));
                                    ar.setNowAmount(cuAcc.getVoucherBalance());
                                    ar.setSign("1");
                                    ar.setOrderNo(o.getOrderNo());
                                    ar.setInfo("拼团专区内累计拼中六次! 广告补贴：" + d);
                                    accountRecordDao.save(ar);
                                    // 改为退完
                                    cir30.setStatus("1"); // 已退
                                    collageInRecord30Dao.update(cir30);
                                    // 把第六次做为新的第一次并发货
                                    // 发货
                                    o.setDealStatus("1");
                                    ordersDao.update(o);
                                    // 创建cir30
                                    CollageInRecord30 newCir30 = new CollageInRecord30();
                                    newCir30.setModeId(o.getCollageModeId());
                                    newCir30.setUserId(cu.getId());
                                    newCir30.setStatus("0");
                                    newCir30.setTotalQuantity(1);
                                    newCir30.setUpdateTime(date);
                                    newCir30.setPayAmount(o.getPayAmount());
                                    collageInRecord30Dao.save(newCir30);
                                    // 添加详情
                                    CollageInRecord30Copy newCir30Copy = new CollageInRecord30Copy();
                                    newCir30Copy.setIsTack("1"); // 提了
                                    newCir30Copy.setCir30Id(newCir30.getId());
                                    newCir30Copy.setOrderNo(o.getOrderNo());
                                    newCir30Copy.setPayAmount(o.getPayAmount());
                                    collageInRecord30CopyDao.save(newCir30Copy);
                                }else{
                                    // 继续累计
                                    cir30.setTotalQuantity(cir30.getTotalQuantity() + 1);
                                    cir30.setPayAmount(cir30.getPayAmount().add(o.getPayAmount()));
                                    collageInRecord30Dao.update(cir30);
                                    // 添加详情
                                    CollageInRecord30Copy newCir30Copy = new CollageInRecord30Copy();
                                    newCir30Copy.setIsTack("0"); // 没有提
                                    newCir30Copy.setCir30Id(cir30.getId());
                                    newCir30Copy.setOrderNo(o.getOrderNo());
                                    newCir30Copy.setPayAmount(o.getPayAmount());
                                    collageInRecord30CopyDao.save(newCir30Copy);
                                    // 代售
                                    o.setDealStatus("7");
                                    ordersDao.update(o);
                                }
                            }
                        }
                    }
                }
            }
        }

        // 处理升级
        handleCollageLevel(customer);

    }

    // TODO 创建订单数据
    public void createOrdersInfo(String outTradeNo, String tradeNo, BigDecimal totalPrice, BigDecimal freight, Product product, String collageModeName,
                                 Integer modeProductId, Integer userId, String consigneeName, String consigneePhone, String fullAddress, String isMember,
                                 SkuRelation sku, Integer buyQuantity, Date date){
        // 创建订单
        Orders orders = new Orders();
        orders.setOrderNo(outTradeNo);
        orders.setTradeNo(tradeNo);
        orders.setTotalAmount(totalPrice);
        orders.setPayAmount(totalPrice);
        orders.setCouponAmount(new BigDecimal(0));
        orders.setFreight(freight);
        orders.setPayStatus("1");
        orders.setPayType("1"); // 挪团，重新下单时，默认为 余额支付
        orders.setDealStatus("6"); // 等待开团
        orders.setRefundStatus("0");
        orders.setPrepayId("");
        orders.setCollageModeId(product.getCollageModeId());
        orders.setModeName(collageModeName);
        orders.setProductId(product.getId());
        orders.setModeProductId(modeProductId);
        orders.setOrderPersonId(userId);
        orders.setConsigneeName(consigneeName);
        orders.setConsigneePhone(consigneePhone);
        orders.setAddress(fullAddress);
        orders.setUpdateTime(date);
        orders.setIsDel("0");
        orders.setIsRun("0");
        if("0".equals(isMember)){
            orders.setLastCollage("0"); // 非会员 默认不拼中为止
        }else{
            orders.setLastCollage("1"); // 会员 默认拼中为止
        }
        orders.setRemarks("无");
        ordersDao.save(orders);
        // 订单详情
        OrderDetails od = new OrderDetails();
        od.setOrderNo(orders.getOrderNo());
        od.setTradeNo(orders.getTradeNo());
        od.setStoreShopId(product.getStoreShopId());
        od.setStoreShopName(storeShopDao.queryById(product.getStoreShopId()).getName());
        od.setClassifyId(product.getClassifyId());
        od.setClassifyName(classifyDao.queryById(product.getClassifyId()).getName());
        od.setProductId(product.getId());
        od.setProductType(product.getType());
        od.setProductImg(product.getImg());
        od.setProductName(product.getName());
        od.setProductCode(product.getProductCode());
        od.setCostPrice(sku.getCostPrice());
        od.setRelationId(sku.getId());
        if(sku.getGroupRelation().contains(",")){
            String[] strings = sku.getGroupRelation().split(",");
            String str = "";
            for(int u = 0; u < strings.length; u++){
                str += specAttributeDao.queryById(Integer.parseInt(strings[u])).getAttributeName() + "，";
            }
            od.setSpecAttributeName(str.substring(0, str.length()-1));
        }else{
            od.setSpecAttributeName(specAttributeDao.queryById(Integer.parseInt(sku.getGroupRelation())).getAttributeName());
        }
        od.setQuantity(buyQuantity);
        od.setUnitPrice(sku.getRetailPrice());
        od.setUpdateTime(date);
        orderDetailsDao.save(od);
    }

    /**
     * 挪订单，重新参团
     * @param o
     */
    public void handleTransferOrders(Orders o){
        OrdersCopy30 oc30 = new OrdersCopy30();
        oc30.setId(o.getId());
        oc30.setOrderNo(o.getOrderNo());
        oc30.setTradeNo(o.getTradeNo());
        oc30.setFreight(o.getFreight());
        oc30.setCollageModeId(o.getCollageModeId());
        oc30.setOrderPersonId(o.getOrderPersonId());
        oc30.setLastCollage(o.getLastCollage());
        oc30.setConsigneeName(o.getConsigneeName());
        oc30.setConsigneePhone(o.getConsigneePhone());
        oc30.setAddress(o.getAddress());
        oc30.setUpdateTime(o.getUpdateTime());
        oc30.setPayAmount(o.getPayAmount());
        oc30.setModeProductId(o.getModeProductId());
        oc30.setProductId(o.getProductId());
        oc30.setSkuId(orderDetailsDao.queryByOrderNo(o.getOrderNo()).getRelationId());
        ordersCopy30Dao.save(oc30);
    }

    /**
     * 处理参与拼团后的等级
     * @param customer
     */
    public void handleCollageLevel(Customer customer){
        Integer levelId = customer.getLevelId();
        if(customer.getLevelId() == 1){ //游客
            customer.setLevelId(2); // 升幼蜂
            if("0".equals(customer.getIsActive())){
                customer.setIsActive("1"); // 激活拼团等级
            }
            customerDao.update(customer);
            if(customer.getParentId() != null && customer.getParentId() != 1198 && customer.getLevelId() != 7){
                Customer parentCustomer = customerDao.queryById(customer.getParentId());
                if(parentCustomer != null){
                    if(parentCustomer.getLevelId() == 1){ // 游客

                    }else if(parentCustomer.getLevelId() == 2){ // 会员
                        // 判断是否直推5个幼蜂同时团队20人
                        int count = customerDao.queryCustomerByLevelIdAndParentId(2, parentCustomer.getId());
                        //int count1 = customerDao.queryCustomerByLevelIdAndParentId_1(1, parentCustomer.getId());
                        if(count >= 5){
                            parentCustomer.setLevelId(3); // 升小蜜蜂
                            customerDao.update(parentCustomer);
                            if(parentCustomer.getParentId() != null && parentCustomer.getParentId() != 1198 && parentCustomer.getLevelId() != 7){
                                Customer top3Customer = customerDao.queryById(parentCustomer.getParentId());
                                if(top3Customer != null){
                                    if(top3Customer.getLevelId() == 1){

                                    }else if(top3Customer.getLevelId() == 2){

                                    }else if(top3Customer.getLevelId() == 3){
                                        // 判断是否直推3个小蜜蜂同时团队100人
                                        int top3Count = customerDao.queryCustomerByLevelIdAndParentId(3, top3Customer.getId());
                                        //int top3Count1 = customerDao.queryCustomerByLevelIdAndParentId_1(1, top3Customer.getId());
                                        if(top3Count >= 3 && top3Customer.getEffective() >= 100) {
                                            top3Customer.setLevelId(4); // 升黄蜂
                                            customerDao.update(top3Customer);
                                            if(top3Customer.getParentId() != null && top3Customer.getParentId() != 1198 && top3Customer.getLevelId() != 7){
                                                Customer top4Customer = customerDao.queryById(top3Customer.getParentId());
                                                if(top4Customer != null){
                                                    if(top4Customer.getLevelId() == 1){

                                                    }else if(top4Customer.getLevelId() == 2){

                                                    }else if(top4Customer.getLevelId() == 3){

                                                    }else if(top4Customer.getLevelId() == 4){
                                                        // 判断是否直推5个黄蜂同时团队1000人
                                                        int top4Count = customerDao.queryCustomerByLevelIdAndParentId(4, top4Customer.getId());
                                                        //int top4Count1 = customerDao.queryCustomerByLevelIdAndParentId_1(1, top4Customer.getId());
                                                        if(top4Count >= 5 && top4Customer.getEffective() >= 1000) {
                                                            top4Customer.setLevelId(5); // 升蜂王
                                                            customerDao.update(top4Customer);
                                                            if(top4Customer.getParentId() != null && top4Customer.getParentId() != 1198 && top4Customer.getLevelId() != 7){
                                                                Customer top5Customer = customerDao.queryById(top4Customer.getParentId());
                                                                if(top5Customer != null){
                                                                    if(top5Customer.getLevelId() == 1){

                                                                    }else if(top5Customer.getLevelId() == 2){

                                                                    }else if(top5Customer.getLevelId() == 3){

                                                                    }else if(top5Customer.getLevelId() == 4){

                                                                    }else if(top5Customer.getLevelId() == 5){
                                                                        // 判断是否直推3个蜂王同时团队3000人
                                                                        int top5Count = customerDao.queryCustomerByLevelIdAndParentId(5, top5Customer.getId());
                                                                        //int top5Count1 = customerDao.queryCustomerByLevelIdAndParentId_1(1, top5Customer.getId());
                                                                        if(top5Count >= 3 && top5Customer.getEffective() >= 3000) {
                                                                            top5Customer.setLevelId(6); // 升蜂皇
                                                                            customerDao.update(top5Customer);
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }else if(top4Customer.getLevelId() == 5){
                                                        // 判断是否直推3个蜂王同时团队3000人
                                                        int top5Count = customerDao.queryCustomerByLevelIdAndParentId(5, top4Customer.getId());
                                                        //int top5Count1 = customerDao.queryCustomerByLevelIdAndParentId_1(1, top4Customer.getId());
                                                        if(top5Count >= 3 && top4Customer.getEffective() >= 3000) {
                                                            top4Customer.setLevelId(6); // 升蜂皇
                                                            customerDao.update(top4Customer);
                                                        }
                                                    }
                                                }
                                            }
                                        }

                                    }else if(top3Customer.getLevelId() == 4){
                                        // 判断是否直推5个黄蜂同时团队1000人
                                        int top4Count = customerDao.queryCustomerByLevelIdAndParentId(4, top3Customer.getId());
                                        //int top4Count1 = customerDao.queryCustomerByLevelIdAndParentId_1(1, top3Customer.getId());
                                        if(top4Count >= 5 && top3Customer.getEffective() >= 1000) {
                                            top3Customer.setLevelId(5); // 升蜂王
                                            customerDao.update(top3Customer);
                                            if(top3Customer.getParentId() != null && top3Customer.getParentId() != 1198 && top3Customer.getLevelId() != 7){
                                                Customer top5Customer = customerDao.queryById(top3Customer.getParentId());
                                                if(top5Customer != null){
                                                    if(top5Customer.getLevelId() == 1){

                                                    }else if(top5Customer.getLevelId() == 2){

                                                    }else if(top5Customer.getLevelId() == 3){

                                                    }else if(top5Customer.getLevelId() == 4){

                                                    }else if(top5Customer.getLevelId() == 5){
                                                        // 判断是否直推3个蜂王同时团队3000人
                                                        int top5Count = customerDao.queryCustomerByLevelIdAndParentId(5, top5Customer.getId());
                                                        //int top5Count1 = customerDao.queryCustomerByLevelIdAndParentId_1(1, top5Customer.getId());
                                                        if(top5Count >= 3 && top5Customer.getEffective() >= 3000) {
                                                            top5Customer.setLevelId(6); // 升蜂皇
                                                            customerDao.update(top5Customer);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }else if(top3Customer.getLevelId() == 5){
                                        // 判断是否直推3个蜂王同时团队3000人
                                        int top5Count = customerDao.queryCustomerByLevelIdAndParentId(5, top3Customer.getId());
                                        //int top5Count1 = customerDao.queryCustomerByLevelIdAndParentId_1(1, top3Customer.getId());
                                        if(top5Count >= 3 && top3Customer.getEffective() >= 3000) {
                                            top3Customer.setLevelId(6); // 升蜂皇
                                            customerDao.update(top3Customer);
                                        }
                                    }
                                }
                            }
                        }

                    }else if(parentCustomer.getLevelId() == 3){ // 小蜜蜂
                        // 判断是否直推3个小蜜蜂同时团队100人
                        int top3Count = customerDao.queryCustomerByLevelIdAndParentId(3, parentCustomer.getId());
                        //int top3Count1 = customerDao.queryCustomerByLevelIdAndParentId_1(1, parentCustomer.getId());
                        if(top3Count >= 3 && parentCustomer.getEffective() >= 100) {
                            parentCustomer.setLevelId(4); // 升黄蜂
                            customerDao.update(parentCustomer);
                            if(parentCustomer.getParentId() != null && parentCustomer.getParentId() != 1198 && parentCustomer.getLevelId() != 7){
                                Customer top4Customer = customerDao.queryById(parentCustomer.getParentId());
                                if(top4Customer != null){
                                    if(top4Customer.getLevelId() == 1){

                                    }else if(top4Customer.getLevelId() == 2){

                                    }else if(top4Customer.getLevelId() == 3){

                                    }else if(top4Customer.getLevelId() == 4){
                                        // 判断是否直推5个黄蜂同时团队1000人
                                        int top4Count = customerDao.queryCustomerByLevelIdAndParentId(4, top4Customer.getId());
                                        //int top4Count1 = customerDao.queryCustomerByLevelIdAndParentId_1(1, top4Customer.getId());
                                        if(top4Count >= 5 && top4Customer.getEffective() >= 1000) {
                                            top4Customer.setLevelId(5); // 升蜂王
                                            customerDao.update(top4Customer);
                                            if(top4Customer.getParentId() != null && top4Customer.getParentId() != 1198 && top4Customer.getLevelId() != 7){
                                                Customer top5Customer = customerDao.queryById(top4Customer.getParentId());
                                                if(top5Customer != null){
                                                    if(top5Customer.getLevelId() == 1){

                                                    }else if(top5Customer.getLevelId() == 2){

                                                    }else if(top5Customer.getLevelId() == 3){

                                                    }else if(top5Customer.getLevelId() == 4){

                                                    }else if(top5Customer.getLevelId() == 5){
                                                        // 判断是否直推3个蜂王同时团队3000人
                                                        int top5Count = customerDao.queryCustomerByLevelIdAndParentId(5, top5Customer.getId());
                                                        //int top5Count1 = customerDao.queryCustomerByLevelIdAndParentId_1(1, top5Customer.getId());
                                                        if(top5Count >= 3 && top5Customer.getEffective() >= 3000) {
                                                            top5Customer.setLevelId(6); // 升蜂皇
                                                            customerDao.update(top5Customer);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }else if(top4Customer.getLevelId() == 5){
                                        // 判断是否直推3个蜂王同时团队3000人
                                        int top5Count = customerDao.queryCustomerByLevelIdAndParentId(5, top4Customer.getId());
                                        //int top5Count1 = customerDao.queryCustomerByLevelIdAndParentId_1(1, top4Customer.getId());
                                        if(top5Count >= 3 && top4Customer.getEffective() >= 3000) {
                                            top4Customer.setLevelId(6); // 升蜂皇
                                            customerDao.update(top4Customer);
                                        }
                                    }
                                }
                            }
                        }
                    }else if(parentCustomer.getLevelId() == 4){ // 黄蜂
                        // 判断是否直推5个黄蜂同时团队1000人
                        int top4Count = customerDao.queryCustomerByLevelIdAndParentId(4, parentCustomer.getId());
                        //int top4Count1 = customerDao.queryCustomerByLevelIdAndParentId_1(1, parentCustomer.getId());
                        if(top4Count >= 5 && parentCustomer.getEffective() >= 1000) {
                            parentCustomer.setLevelId(5); // 升蜂王
                            customerDao.update(parentCustomer);
                            if(parentCustomer.getParentId() != null && parentCustomer.getParentId() != 1198 && parentCustomer.getLevelId() != 7){
                                Customer top5Customer = customerDao.queryById(parentCustomer.getParentId());
                                if(top5Customer != null){
                                    if(top5Customer.getLevelId() == 1){

                                    }else if(top5Customer.getLevelId() == 2){

                                    }else if(top5Customer.getLevelId() == 3){

                                    }else if(top5Customer.getLevelId() == 4){

                                    }else if(top5Customer.getLevelId() == 5){
                                        // 判断是否直推3个蜂王同时团队3000人
                                        int top5Count = customerDao.queryCustomerByLevelIdAndParentId(5, top5Customer.getId());
                                        //int top5Count1 = customerDao.queryCustomerByLevelIdAndParentId_1(1, top5Customer.getId());
                                        if(top5Count >= 3 && top5Customer.getEffective() >= 3000) {
                                            top5Customer.setLevelId(6); // 升蜂皇
                                            customerDao.update(top5Customer);
                                        }
                                    }
                                }
                            }
                        }
                    }else if(parentCustomer.getLevelId() == 5){ // 蜂王
                        // 判断是否直推3个蜂王同时团队3000人
                        int top5Count = customerDao.queryCustomerByLevelIdAndParentId(5, parentCustomer.getId());
                        //int top5Count1 = customerDao.queryCustomerByLevelIdAndParentId_1(1, parentCustomer.getId());
                        if(top5Count >= 3 && parentCustomer.getEffective() >= 3000) {
                            parentCustomer.setLevelId(6); // 升蜂皇
                            customerDao.update(parentCustomer);
                        }
                    }
                }
            }

        }else if(customer.getLevelId() == 2){ // 幼蜂

        }else if(customer.getLevelId() == 3){ // 小蜜蜂

        }else if(customer.getLevelId() == 4){ // 黄蜂

        }else if(customer.getLevelId() == 5){ // 蜂王

        }else if(customer.getLevelId() == 6){ // 蜂皇

        }else if(customer.getLevelId() == 7){ // 公司

        }

        // 添加有效人数
        if(levelId == 1){
            addEffectiveQuantity(customer);
        }else if(levelId == 4 || levelId == 5 || levelId == 6){
            List<Orders> ordersList = ordersDao.queryOrdersBy30Or40(customer.getId());
            if(ordersList.size() < 1){
                addEffectiveQuantity(customer);
            }
        }

    }

    @Override
    public void handleAgainOrders_wxPay(Orders transmitOrders) {
        CollageMode collageMode = collageModeDao.queryById(transmitOrders.getCollageModeId());
        Product product = productDao.queryById(transmitOrders.getProductId());
        //OrderDetails orderDetails = orderDetailsDao.queryByOrderNo(transmitOrders.getOrderNo());
        Customer customer = customerDao.queryById(transmitOrders.getOrderPersonId());
        Date date = transmitOrders.getUpdateTime();
        UserCoupon userCoupon = userCouponDao.queryByUserIdAndCouponId(customer.getId(), product.getCouponId());
        if("0".equals(customer.getIsMember())){
            // 扣除参团券1张
            userCoupon.setQuantity(userCoupon.getQuantity() - 1);
            userCouponDao.update(userCoupon);
        }else if("1".equals(customer.getIsMember())){
            // 扣除参团券15张
            userCoupon.setQuantity(0);
            userCouponDao.update(userCoupon);
        }
        // 处理团信息
        ModeProduct modeProduct = modeProductDao.queryByProductIdByStatus(product.getId());
        if(modeProduct == null){
            // 创建新团
            ModeProduct newMP = new ModeProduct();
            newMP.setModeId(product.getCollageModeId());
            newMP.setProductId(product.getId());
            newMP.setStatus("0");
            newMP.setUpdateTime(date);
            modeProductDao.save(newMP);
            // 加入到当前团
            CollageUser newCU = new CollageUser();
            newCU.setModeProductId(newMP.getId());
            newCU.setUserId(customer.getId());
            newCU.setUpdateTime(date);
            collageUserDao.save(newCU);

            // 将订单加入进去
            transmitOrders.setModeProductId(newMP.getId());
            ordersDao.update(transmitOrders);

        }else{
            int quantity = collageMode.getPeopleQuantity(); // 20
            // 查询现在这个产品团里面有多少人了
            List<CollageUser> collageUserList = collageUserDao.queryByModeProductId(modeProduct.getId());
            int rowSize = collageUserList.size(); // 已参与人数
            if(rowSize < (quantity -1)){
                // 直接加入
                CollageUser newCU = new CollageUser();
                newCU.setModeProductId(modeProduct.getId());
                newCU.setUserId(customer.getId());
                newCU.setUpdateTime(date);
                collageUserDao.save(newCU);

                // 将订单加入进去
                transmitOrders.setModeProductId(modeProduct.getId());
                ordersDao.update(transmitOrders);

            }else if(rowSize == (quantity -1)){ // 需要重新生成新团
                // 修改老团,拼团成功
                modeProduct.setStatus("1");
                modeProductDao.update(modeProduct);
                // 加入当前团
                CollageUser newCU = new CollageUser();
                newCU.setModeProductId(modeProduct.getId());
                newCU.setUserId(customer.getId());
                newCU.setUpdateTime(date);
                collageUserDao.save(newCU);
                // 创建新团
                ModeProduct newMo = new ModeProduct();
                newMo.setUpdateTime(new Date());
                newMo.setProductId(product.getId());
                newMo.setModeId(collageMode.getId());
                newMo.setStatus("0");
                modeProductDao.save(newMo);

                // 将订单加入进去
                transmitOrders.setModeProductId(modeProduct.getId());
                ordersDao.update(transmitOrders);

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String today = sdf.format(new Date());

                // 获取当前团的订单信息，处理哪些需要发货，哪些需要退款，哪些需要挪团
                List<Orders> ordersList = ordersDao.queryOrdersByCollageProductId(modeProduct.getId());
                int ordersSize = ordersList.size();
                if(ordersSize > 0){
                    for(int u = 0; u < ordersSize; u++){
                        Orders o = ordersList.get(u);
                        Customer cu = customerDao.queryById(o.getOrderPersonId());
                        OrderDetails od = orderDetailsDao.queryByOrderNo(o.getOrderNo());
                        Product p = productDao.queryById(od.getProductId());
                        if ("1".equals(cu.getIsMember())) {
                            // 会员今日每中 直接一波流出局
                            // todo 那29元 修改为14
                            BigDecimal profit = BigDecimal.valueOf(14);
                            if (p.getCollageModeId() == 40) {
                                // todo 拿走58元 修改为28
                                profit = BigDecimal.valueOf(28);
                            }
                            CollageTotalVoucher ctv = collageTotalVoucherDao.queryByIdsAndTodayTime(cu.getId(), p.getCollageModeId(), today);
                            if (ctv == null) {
                                CollageTotalVoucher newCtv = new CollageTotalVoucher();
                                newCtv.setAmount(profit);
                                newCtv.setModeId(p.getCollageModeId());
                                newCtv.setStatus("0");
                                newCtv.setUpdateTime(date);
                                newCtv.setUserId(cu.getId());
                                collageTotalVoucherDao.save(newCtv);
                                // 分润
                                ordersService.handleCollageProfit30(cu.getId(), profit, o.getOrderNo());
                            }

                            // 获取累计中奖记录数据
                            CollageInRecord30 cir30 = collageInRecord30Dao.queryByModeIdAndUserId(o.getCollageModeId(), cu.getId());
                            if (cir30 == null) {
                                // 创建cir30
                                CollageInRecord30 newCir30 = new CollageInRecord30();
                                newCir30.setModeId(o.getCollageModeId());
                                newCir30.setUserId(cu.getId());
                                newCir30.setStatus("0");
                                newCir30.setTotalQuantity(1);
                                newCir30.setUpdateTime(date);
                                newCir30.setPayAmount(o.getPayAmount());
                                collageInRecord30Dao.save(newCir30);
                                // 添加详情
                                CollageInRecord30Copy newCir30Copy = new CollageInRecord30Copy();
                                newCir30Copy.setIsTack("1"); // 提了
                                newCir30Copy.setCir30Id(newCir30.getId());
                                newCir30Copy.setOrderNo(o.getOrderNo());
                                newCir30Copy.setPayAmount(o.getPayAmount());
                                collageInRecord30CopyDao.save(newCir30Copy);
                                // 发货
                                o.setDealStatus("1");
                                ordersDao.update(o);
                            } else {
                                // 判断是否需要返钱
                                if (cir30.getTotalQuantity() == 5) {
                                    // 开始退钱，退未提货的钱
                                    Account cuAcc = accountDao.queryAccountByUserId(cu.getId());
                                    Double d = 0.0;
                                    List<CollageInRecord30Copy> cir30CopyList = collageInRecord30CopyDao.queryByCir30Id(cir30.getId());
                                    if (cir30CopyList.size() > 0) {
                                        for (CollageInRecord30Copy cir30Copy : cir30CopyList) {
                                            cir30Copy.setIsTack("1"); // 修改为已提货
                                            d += cir30Copy.getPayAmount().doubleValue();
                                            collageInRecord30CopyDao.update(cir30Copy);
                                            Orders orders = ordersDao.queryByOrderNo(cir30Copy.getOrderNo());
                                            orders.setDealStatus("3"); // 已完成
                                            orders.setConfirmTime(new Date());
                                            orders.setRemarks("拼团专区内累计拼中六次! 当前订单自动完成");
                                            ordersDao.update(orders);
                                        }
                                    }
                                    cuAcc.setVoucherBalance(cuAcc.getVoucherBalance().add(BigDecimal.valueOf(d))); //余额
                                    accountDao.update(cuAcc);
                                    // 加个ar记录
                                    AccountRecord ar = new AccountRecord();
                                    ar.setIntoUserId(cu.getId());
                                    ar.setUpdateTime(date);
                                    ar.setType("0");
                                    ar.setAmount(BigDecimal.valueOf(d));
                                    ar.setNowAmount(cuAcc.getVoucherBalance());
                                    ar.setSign("1");
                                    ar.setOrderNo(o.getOrderNo());
                                    ar.setInfo("拼团专区内累计拼中六次! 广告补贴：" + d);
                                    accountRecordDao.save(ar);
                                    // 改为退完
                                    cir30.setStatus("1"); // 已退
                                    collageInRecord30Dao.update(cir30);
                                    // 把第六次做为新的第一次并发货
                                    // 发货
                                    o.setDealStatus("1");
                                    ordersDao.update(o);
                                    // 创建cir30
                                    CollageInRecord30 newCir30 = new CollageInRecord30();
                                    newCir30.setModeId(o.getCollageModeId());
                                    newCir30.setUserId(cu.getId());
                                    newCir30.setStatus("0");
                                    newCir30.setTotalQuantity(1);
                                    newCir30.setUpdateTime(date);
                                    newCir30.setPayAmount(o.getPayAmount());
                                    collageInRecord30Dao.save(newCir30);
                                    // 添加详情
                                    CollageInRecord30Copy newCir30Copy = new CollageInRecord30Copy();
                                    newCir30Copy.setIsTack("1"); // 提了
                                    newCir30Copy.setCir30Id(newCir30.getId());
                                    newCir30Copy.setOrderNo(o.getOrderNo());
                                    newCir30Copy.setPayAmount(o.getPayAmount());
                                    collageInRecord30CopyDao.save(newCir30Copy);
                                } else {
                                    // 继续累计
                                    cir30.setTotalQuantity(cir30.getTotalQuantity() + 1);
                                    cir30.setPayAmount(cir30.getPayAmount().add(o.getPayAmount()));
                                    collageInRecord30Dao.update(cir30);
                                    // 添加详情
                                    CollageInRecord30Copy newCir30Copy = new CollageInRecord30Copy();
                                    newCir30Copy.setIsTack("0"); // 没有提
                                    newCir30Copy.setCir30Id(cir30.getId());
                                    newCir30Copy.setOrderNo(o.getOrderNo());
                                    newCir30Copy.setPayAmount(o.getPayAmount());
                                    collageInRecord30CopyDao.save(newCir30Copy);
                                    // 代售
                                    o.setDealStatus("7");
                                    ordersDao.update(o);
                                }
                            }
                        }
                    }
                }
            }
        }

        // 处理升级
        handleCollageLevel(customer);

    }

    @Override
    public void handleAgainOrders_aliPay(Orders transmitOrders) {

    }

    public void addEffectiveQuantity(Customer customer){
        if(customer.getParentId() != null && customer.getParentId() != 1198){
            Customer cu1 = customerDao.queryById(customer.getParentId());
            if(cu1 != null){
                cu1.setEffective(cu1.getEffective() + 1);
                customerDao.update(cu1);

                if(cu1.getParentId() != null && cu1.getParentId() != 1198){
                    Customer cu2 = customerDao.queryById(cu1.getParentId());
                    if(cu2 != null){
                        cu2.setEffective(cu2.getEffective() + 1);
                        customerDao.update(cu2);

                        if(cu2.getParentId() != null && cu2.getParentId() != 1198){
                            Customer cu3 = customerDao.queryById(cu2.getParentId());
                            if(cu3 != null){
                                cu3.setEffective(cu3.getEffective() + 1);
                                customerDao.update(cu3);

                                if(cu3.getParentId() != null && cu3.getParentId() != 1198){
                                    Customer cu4 = customerDao.queryById(cu3.getParentId());
                                    if(cu4 != null){
                                        cu4.setEffective(cu4.getEffective() + 1);
                                        customerDao.update(cu4);

                                        if(cu4.getParentId() != null && cu4.getParentId() != 1198){
                                            Customer cu5 = customerDao.queryById(cu4.getParentId());
                                            if(cu5 != null){
                                                cu5.setEffective(cu5.getEffective() + 1);
                                                customerDao.update(cu5);

                                                if(cu5.getParentId() != null && cu5.getParentId() != 1198){
                                                    Customer cu6 = customerDao.queryById(cu5.getParentId());
                                                    if(cu6 != null){
                                                        cu6.setEffective(cu6.getEffective() + 1);
                                                        customerDao.update(cu6);

                                                        if(cu6.getParentId() != null && cu6.getParentId() != 1198){
                                                            Customer cu7 = customerDao.queryById(cu6.getParentId());
                                                            if(cu7 != null){
                                                                cu7.setEffective(cu7.getEffective() + 1);
                                                                customerDao.update(cu7);

                                                                if(cu7.getParentId() != null && cu7.getParentId() != 1198){
                                                                    Customer cu8 = customerDao.queryById(cu7.getParentId());
                                                                    if(cu8 != null){
                                                                        cu8.setEffective(cu8.getEffective() + 1);
                                                                        customerDao.update(cu8);

                                                                        if(cu8.getParentId() != null && cu8.getParentId() != 1198){
                                                                            Customer cu9 = customerDao.queryById(cu8.getParentId());
                                                                            if(cu9 != null){
                                                                                cu9.setEffective(cu9.getEffective() + 1);
                                                                                customerDao.update(cu9);

                                                                                if(cu9.getParentId() != null && cu9.getParentId() != 1198){
                                                                                    Customer cu10 = customerDao.queryById(cu9.getParentId());
                                                                                    if(cu10 != null){
                                                                                        cu10.setEffective(cu10.getEffective() + 1);
                                                                                        customerDao.update(cu10);

                                                                                        if(cu10.getParentId() != null && cu10.getParentId() != 1198){
                                                                                            Customer cu11 = customerDao.queryById(cu10.getParentId());
                                                                                            if(cu11 != null){
                                                                                                cu11.setEffective(cu11.getEffective() + 1);
                                                                                                customerDao.update(cu11);

                                                                                                if(cu11.getParentId() != null && cu11.getParentId() != 1198){
                                                                                                    Customer cu12 = customerDao.queryById(cu11.getParentId());
                                                                                                    if(cu12 != null){
                                                                                                        cu12.setEffective(cu12.getEffective() + 1);
                                                                                                        customerDao.update(cu12);

                                                                                                        if(cu12.getParentId() != null && cu12.getParentId() != 1198){
                                                                                                            Customer cu13 = customerDao.queryById(cu12.getParentId());
                                                                                                            if(cu13 != null){
                                                                                                                cu13.setEffective(cu13.getEffective() + 1);
                                                                                                                customerDao.update(cu13);

                                                                                                                if(cu13.getParentId() != null && cu13.getParentId() != 1198){
                                                                                                                    Customer cu14 = customerDao.queryById(cu13.getParentId());
                                                                                                                    if(cu14 != null){
                                                                                                                        cu14.setEffective(cu14.getEffective() + 1);
                                                                                                                        customerDao.update(cu14);

                                                                                                                        if(cu14.getParentId() != null && cu14.getParentId() != 1198){
                                                                                                                            Customer cu15 = customerDao.queryById(cu14.getParentId());
                                                                                                                            if(cu15 != null){
                                                                                                                                cu15.setEffective(cu15.getEffective() + 1);
                                                                                                                                customerDao.update(cu15);

                                                                                                                                if(cu15.getParentId() != null && cu15.getParentId() != 1198){
                                                                                                                                    Customer cu16 = customerDao.queryById(cu15.getParentId());
                                                                                                                                    if(cu16 != null){
                                                                                                                                        cu16.setEffective(cu16.getEffective() + 1);
                                                                                                                                        customerDao.update(cu16);

                                                                                                                                        if(cu16.getParentId() != null && cu16.getParentId() != 1198){
                                                                                                                                            Customer cu17 = customerDao.queryById(cu16.getParentId());
                                                                                                                                            if(cu17 != null){
                                                                                                                                                cu17.setEffective(cu17.getEffective() + 1);
                                                                                                                                                customerDao.update(cu17);

                                                                                                                                                if(cu17.getParentId() != null && cu17.getParentId() != 1198){
                                                                                                                                                    Customer cu18 = customerDao.queryById(cu17.getParentId());
                                                                                                                                                    if(cu18 != null){
                                                                                                                                                        cu18.setEffective(cu18.getEffective() + 1);
                                                                                                                                                        customerDao.update(cu18);

                                                                                                                                                        if(cu18.getParentId() != null && cu18.getParentId() != 1198){
                                                                                                                                                            Customer cu19 = customerDao.queryById(cu18.getParentId());
                                                                                                                                                            if(cu19 != null){
                                                                                                                                                                cu19.setEffective(cu19.getEffective() + 1);
                                                                                                                                                                customerDao.update(cu19);

                                                                                                                                                                if(cu19.getParentId() != null && cu19.getParentId() != 1198){
                                                                                                                                                                    Customer cu20 = customerDao.queryById(cu19.getParentId());
                                                                                                                                                                    if(cu20 != null){
                                                                                                                                                                        cu20.setEffective(cu20.getEffective() + 1);
                                                                                                                                                                        customerDao.update(cu20);

                                                                                                                                                                        if(cu20.getParentId() != null && cu20.getParentId() != 1198){
                                                                                                                                                                            Customer cu21 = customerDao.queryById(cu20.getParentId());
                                                                                                                                                                            if(cu21 != null){
                                                                                                                                                                                cu21.setEffective(cu21.getEffective() + 1);
                                                                                                                                                                                customerDao.update(cu21);

                                                                                                                                                                                if(cu21.getParentId() != null && cu21.getParentId() != 1198){
                                                                                                                                                                                    Customer cu22 = customerDao.queryById(cu21.getParentId());
                                                                                                                                                                                    if(cu22 != null){
                                                                                                                                                                                        cu22.setEffective(cu22.getEffective() + 1);
                                                                                                                                                                                        customerDao.update(cu22);

                                                                                                                                                                                        if(cu22.getParentId() != null && cu22.getParentId() != 1198){
                                                                                                                                                                                            Customer cu23 = customerDao.queryById(cu22.getParentId());
                                                                                                                                                                                            if(cu23 != null){
                                                                                                                                                                                                cu23.setEffective(cu23.getEffective() + 1);
                                                                                                                                                                                                customerDao.update(cu23);

                                                                                                                                                                                                if(cu23.getParentId() != null && cu23.getParentId() != 1198){
                                                                                                                                                                                                    Customer cu24 = customerDao.queryById(cu23.getParentId());
                                                                                                                                                                                                    if(cu24 != null){
                                                                                                                                                                                                        cu24.setEffective(cu24.getEffective() + 1);
                                                                                                                                                                                                        customerDao.update(cu24);

                                                                                                                                                                                                        if(cu24.getParentId() != null && cu24.getParentId() != 1198){
                                                                                                                                                                                                            Customer cu25 = customerDao.queryById(cu24.getParentId());
                                                                                                                                                                                                            if(cu25 != null){
                                                                                                                                                                                                                cu25.setEffective(cu25.getEffective() + 1);
                                                                                                                                                                                                                customerDao.update(cu25);

                                                                                                                                                                                                                if(cu25.getParentId() != null && cu25.getParentId() != 1198){
                                                                                                                                                                                                                    Customer cu26 = customerDao.queryById(cu25.getParentId());
                                                                                                                                                                                                                    if(cu26 != null){
                                                                                                                                                                                                                        cu26.setEffective(cu26.getEffective() + 1);
                                                                                                                                                                                                                        customerDao.update(cu26);

                                                                                                                                                                                                                        if(cu26.getParentId() != null && cu26.getParentId() != 1198){
                                                                                                                                                                                                                            Customer cu27 = customerDao.queryById(cu26.getParentId());
                                                                                                                                                                                                                            if(cu27 != null){
                                                                                                                                                                                                                                cu27.setEffective(cu27.getEffective() + 1);
                                                                                                                                                                                                                                customerDao.update(cu27);

                                                                                                                                                                                                                                if(cu27.getParentId() != null && cu27.getParentId() != 1198){
                                                                                                                                                                                                                                    Customer cu28 = customerDao.queryById(cu27.getParentId());
                                                                                                                                                                                                                                    if(cu28 != null){
                                                                                                                                                                                                                                        cu28.setEffective(cu28.getEffective() + 1);
                                                                                                                                                                                                                                        customerDao.update(cu28);

                                                                                                                                                                                                                                        if(cu28.getParentId() != null && cu28.getParentId() != 1198){
                                                                                                                                                                                                                                            Customer cu29 = customerDao.queryById(cu28.getParentId());
                                                                                                                                                                                                                                            if(cu29 != null){
                                                                                                                                                                                                                                                cu29.setEffective(cu29.getEffective() + 1);
                                                                                                                                                                                                                                                customerDao.update(cu29);

                                                                                                                                                                                                                                                if(cu29.getParentId() != null && cu29.getParentId() != 1198){
                                                                                                                                                                                                                                                    Customer cu30 = customerDao.queryById(cu29.getParentId());
                                                                                                                                                                                                                                                    if(cu30 != null){
                                                                                                                                                                                                                                                        cu30.setEffective(cu30.getEffective() + 1);
                                                                                                                                                                                                                                                        customerDao.update(cu30);

                                                                                                                                                                                                                                                        if(cu30.getParentId() != null && cu30.getParentId() != 1198){
                                                                                                                                                                                                                                                            Customer cu31 = customerDao.queryById(cu30.getParentId());
                                                                                                                                                                                                                                                            if(cu31 != null){
                                                                                                                                                                                                                                                                cu31.setEffective(cu31.getEffective() + 1);
                                                                                                                                                                                                                                                                customerDao.update(cu31);

                                                                                                                                                                                                                                                                if(cu31.getParentId() != null && cu31.getParentId() != 1198){
                                                                                                                                                                                                                                                                    Customer cu32 = customerDao.queryById(cu31.getParentId());
                                                                                                                                                                                                                                                                    if(cu32 != null){
                                                                                                                                                                                                                                                                        cu32.setEffective(cu32.getEffective() + 1);
                                                                                                                                                                                                                                                                        customerDao.update(cu32);

                                                                                                                                                                                                                                                                        if(cu32.getParentId() != null && cu32.getParentId() != 1198){
                                                                                                                                                                                                                                                                            Customer cu33 = customerDao.queryById(cu32.getParentId());
                                                                                                                                                                                                                                                                            if(cu33 != null){
                                                                                                                                                                                                                                                                                cu33.setEffective(cu33.getEffective() + 1);
                                                                                                                                                                                                                                                                                customerDao.update(cu33);

                                                                                                                                                                                                                                                                                if(cu33.getParentId() != null && cu33.getParentId() != 1198){
                                                                                                                                                                                                                                                                                    Customer cu34 = customerDao.queryById(cu33.getParentId());
                                                                                                                                                                                                                                                                                    if(cu34 != null){
                                                                                                                                                                                                                                                                                        cu34.setEffective(cu34.getEffective() + 1);
                                                                                                                                                                                                                                                                                        customerDao.update(cu34);

                                                                                                                                                                                                                                                                                        if(cu34.getParentId() != null && cu34.getParentId() != 1198){
                                                                                                                                                                                                                                                                                            Customer cu35 = customerDao.queryById(cu34.getParentId());
                                                                                                                                                                                                                                                                                            if(cu35 != null){
                                                                                                                                                                                                                                                                                                cu35.setEffective(cu35.getEffective() + 1);
                                                                                                                                                                                                                                                                                                customerDao.update(cu35);

                                                                                                                                                                                                                                                                                                if(cu35.getParentId() != null && cu35.getParentId() != 1198){
                                                                                                                                                                                                                                                                                                    Customer cu36 = customerDao.queryById(cu35.getParentId());
                                                                                                                                                                                                                                                                                                    if(cu36 != null){
                                                                                                                                                                                                                                                                                                        cu36.setEffective(cu36.getEffective() + 1);
                                                                                                                                                                                                                                                                                                        customerDao.update(cu36);

                                                                                                                                                                                                                                                                                                        if(cu36.getParentId() != null && cu36.getParentId() != 1198){
                                                                                                                                                                                                                                                                                                            Customer cu37 = customerDao.queryById(cu36.getParentId());
                                                                                                                                                                                                                                                                                                            if(cu37 != null){
                                                                                                                                                                                                                                                                                                                cu37.setEffective(cu37.getEffective() + 1);
                                                                                                                                                                                                                                                                                                                customerDao.update(cu37);

                                                                                                                                                                                                                                                                                                                if(cu37.getParentId() != null && cu37.getParentId() != 1198){
                                                                                                                                                                                                                                                                                                                    Customer cu38 = customerDao.queryById(cu37.getParentId());
                                                                                                                                                                                                                                                                                                                    if(cu38 != null){
                                                                                                                                                                                                                                                                                                                        cu38.setEffective(cu38.getEffective() + 1);
                                                                                                                                                                                                                                                                                                                        customerDao.update(cu38);

                                                                                                                                                                                                                                                                                                                        if(cu38.getParentId() != null && cu38.getParentId() != 1198){
                                                                                                                                                                                                                                                                                                                            Customer cu39 = customerDao.queryById(cu38.getParentId());
                                                                                                                                                                                                                                                                                                                            if(cu39 != null){
                                                                                                                                                                                                                                                                                                                                cu39.setEffective(cu39.getEffective() + 1);
                                                                                                                                                                                                                                                                                                                                customerDao.update(cu39);

                                                                                                                                                                                                                                                                                                                                if(cu39.getParentId() != null && cu39.getParentId() != 1198){
                                                                                                                                                                                                                                                                                                                                    Customer cu40 = customerDao.queryById(cu39.getParentId());
                                                                                                                                                                                                                                                                                                                                    if(cu40 != null){
                                                                                                                                                                                                                                                                                                                                        cu40.setEffective(cu40.getEffective() + 1);
                                                                                                                                                                                                                                                                                                                                        customerDao.update(cu40);

                                                                                                                                                                                                                                                                                                                                        if(cu40.getParentId() != null && cu40.getParentId() != 1198){
                                                                                                                                                                                                                                                                                                                                            Customer cu41 = customerDao.queryById(cu40.getParentId());
                                                                                                                                                                                                                                                                                                                                            if(cu41 != null){
                                                                                                                                                                                                                                                                                                                                                cu41.setEffective(cu41.getEffective() + 1);
                                                                                                                                                                                                                                                                                                                                                customerDao.update(cu41);
                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                }
                                                                                                                                                                                                                            }
                                                                                                                                                                                                                        }
                                                                                                                                                                                                                    }
                                                                                                                                                                                                                }
                                                                                                                                                                                                            }
                                                                                                                                                                                                        }
                                                                                                                                                                                                    }
                                                                                                                                                                                                }
                                                                                                                                                                                            }
                                                                                                                                                                                        }
                                                                                                                                                                                    }
                                                                                                                                                                                }
                                                                                                                                                                            }
                                                                                                                                                                        }
                                                                                                                                                                    }
                                                                                                                                                                }
                                                                                                                                                            }
                                                                                                                                                        }
                                                                                                                                                    }
                                                                                                                                                }
                                                                                                                                            }
                                                                                                                                        }
                                                                                                                                    }
                                                                                                                                }
                                                                                                                            }
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

}
